package fastjson;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.util.*;

public class FastJsonTest {
//    序列化就是指 把JavaBean对象转成JSON格式的字符串。
//    com.alibaba.fastjson.JSON提供了许多方法（多态）实现序列化。
    public static void main(String[] args) {
//        List<String> arrayList = Arrays.asList("fang","dhjs","fajs");
//        String objJson = JSON.toJSONString(arrayList);
//        System.out.println(objJson);
//
//        //格式化Json
//        String strJson = JSON.toJSONString(arrayList,true);
//        System.out.println(strJson);

        //Tue Jun 01 19:51:22 CST 2021
//        dateTest();
//        deserializerTest();
//        objectAndArray();
        User user = new User();
        System.out.println(user);

    }
    public static void dateTest() {
        Date date = new Date();
        System.out.println(date);
        System.out.println(JSON.toJSONString(date));
        System.out.println(JSON.toJSONStringWithDateFormat(date, "yyyy-MM-dd HH:mm:ss.SSS"));
    }
    public static void deserializerTest() {
        ArrayList<Map<String, String>> arrayList1 = new ArrayList<>();
        Map<String, String> map1 = new HashMap<>();
        map1.put("aaaa","aaaa");
        map1.put("bbbb","bbbb");
        map1.put("cccc","cccc");
        HashMap<String, String> map2 = new HashMap<>();
        map2.put("fang","12");
        map2.put("qing","14");
        arrayList1.add(map1);
        arrayList1.add(map2);
//        [{aaaa=aaaa, bbbb=bbbb, cccc=cccc}, {qing=14, fang=12}]
//        [{aaaa=aaaa, bbbb=bbbb, cccc=cccc}, {qing=14, fang=12}]
        String strJson = JSON.toJSONString(arrayList1,true);
        List<Map> list1 = JSON.parseArray(strJson, Map.class);

        System.out.println(list1);
        System.out.println(list1.toString());

        String strJson1 = JSON.toJSONString(map1,true);
//        map直接打印中间是等于，
//        {aaaa=aaaa, bbbb=bbbb, cccc=cccc}
//        {
//            "aaaa":"aaaa",
//            "bbbb":"bbbb",
//            "cccc":"cccc"
//        }
        System.out.println(map1);
        System.out.println(strJson1);




        //增强for遍历list
        for(Map<String, Object> map : list1){
            System.out.println(map.get("aaaa"));
            System.out.println(map.get("fang"));
        }
    }

    /**
     * JSONObject，JSONArray是JSON的两个子类。
     * JSONObject相当于Map<String, Object>，
     * JSONArray相当于List<Object>。
     */
    public static void objectAndArray() {
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("aa","aa");
        hashMap.put("bb","bb");
        hashMap.put("cc","cc");
        //JSONObject要求得参数类型HashMap<String, Object>,前面是String，后面是Object
        JSONObject jsonObject = new JSONObject(hashMap);
        jsonObject.put("dd","dd");
        //三者打印出来得结果一样{"aa":"aa","bb":"bb","cc":"cc","dd":"dd"}
        System.out.println(jsonObject);
        System.out.println(jsonObject.toJSONString());
        System.out.println(jsonObject.toString());

        ArrayList<Map<String, Object>> maps = new ArrayList<>();
        HashMap<String, Object> map1 = new HashMap<>();
        map1.put("aa","aa");
        map1.put("bb","bb");
        map1.put("cc","cc");
        HashMap<String, Object> map2 = new HashMap<>();
        map2.put("11","22");
        map2.put("33","44");
        maps.add(map1);
        maps.add(map2);
        System.out.println(JSON.toJSONString(maps));
        JSONArray j = JSONArray.parseArray(JSON.toJSONString(maps));
        for (int i = 0; i < j.size(); i++) {
            System.out.println(j.get(i));
        }
        }
}
